<?php


namespace App\backend\controllers\goods;


use App\backend\controllers\AdminBaseController;
use App\backend\models\GoodsTagModel;
use App\backend\services\GoodsTagServices;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class GoodsTagController extends AdminBaseController
{
    protected $class_name = 'GoodsTag';

    /**
     * 商品标签列表
     */
    public function getList(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsTagModel::rules(),
            GoodsTagModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsTagServices::getList($data);
        return successJson('获取成功!', $list);
    }

    /**
     * 添加/编辑商品标签
     */
    public function goodsTagModify(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsTagModel::modify_rules(),
            GoodsTagModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsTagServices::goodsTagModify($data);
        if (!$list) {
            return errorJson('操作失败！', []);
        }
        return successJson('操作成功！', []);
    }

    /**
     * 删除商品标签
     */
    public function goodsTagDelete(Request $request)
    {
        $data = $request->all();
        $validator = Validator::make(
            $data,
            GoodsTagModel::updated_rules(),
            GoodsTagModel::message()
        );
        if ($validator->fails()) {
            $msg = $validator->errors()->all();
            return errorJson($msg[0], []);
        }
        $list = GoodsTagServices::goodsTagDelete($data);
        return successJson('操作成功！', $list);
    }

}
